package com.supreme.studyserver.service;

import org.springframework.stereotype.Service;

import java.lang.management.*;
import java.util.List;

@Service
public class ManagementService {

    public void getMemoryMXBeanInfo() {
        //获取整个虚拟机内存使用情况
        System.out.println("\n=======================获取整个虚拟机内存使用情况============================: ");
        final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        final MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        System.out.println("Heap Memory Usage: " + memoryMXBean.getHeapMemoryUsage());
        System.out.println("Non-Heap Memory Usage: " + memoryMXBean.getNonHeapMemoryUsage());
        System.out.println("INIT HEAP: " + heapMemoryUsage.getInit() / 1024 / 1024);
        System.out.println("MAX HEAP: " + heapMemoryUsage.getMax() / 1024 / 1024);
        System.out.println("USE HEAP: " + heapMemoryUsage.getUsed() / 1024 / 1024);
        System.out.println("COMMITTED HEAP: " + heapMemoryUsage.getCommitted() / 1024 / 1024);

        System.out.println("\n=======================通过java来获取相关系统状态============================");
        //Java 虚拟机中的内存总量,以字节为单位
        long i = Runtime.getRuntime().totalMemory() / 1024 / 1024;
        System.out.println("总的内存量 i is: " + i);
        //Java 虚拟机中的空闲内存量
        final long j = Runtime.getRuntime().freeMemory() / 1024 / 1224;
        System.out.println("空闲内存量 j is: " + j);
        System.out.println("最大内存量 is: " + Runtime.getRuntime().maxMemory() / 1024 / 1024);

        System.out.println("\n=======================VM ARGUMENTS============================");
        RuntimeMXBean rmb = ManagementFactory.getRuntimeMXBean();
        rmb.getInputArguments().forEach(System.out::println);
    }

    public void getOperatingSystemMXBeanInfo() {
        //获取操作系统相关信息
        System.out.println("\n=======================OperatingSystemMXBean 获取操作系统相关信息============================");
        final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        System.out.println("osm.getArch(): " + operatingSystemMXBean.getArch());
        System.out.println("osm.getAvailableProcessors(): " + operatingSystemMXBean.getAvailableProcessors());
        System.out.println("osm.getName(): " + operatingSystemMXBean.getName());
        System.out.println("osm.getVersion(): " + operatingSystemMXBean.getVersion());
    }

    public void getThreadMXBeanInfo() {
        //获取各个线程的各种状态，CPU 占用情况，以及整个系统中的线程状况
        System.out.println("\n=======================ThreadMXBean 获取各个线程的各种状态，CPU 占用情况，以及整个系统中的线程状况============================ ");
        final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        System.out.println("getThreadCount: " + threadMXBean.getThreadCount());
        System.out.println("getPeakThreadCount: " + threadMXBean.getPeakThreadCount());
        System.out.println("getCurrentThreadCpuTime: " + threadMXBean.getCurrentThreadCpuTime());
        System.out.println("getDaemonThreadCount: " + threadMXBean.getDaemonThreadCount());
        System.out.println("getCurrentThreadUserTime: " + threadMXBean.getCurrentThreadUserTime());
    }

    public void getOtherManagementInfo() {
        //当前编译器情况
        System.out.println("\n=======================CompilationMXBean 当前编译器情况============================ ");
        CompilationMXBean gm = ManagementFactory.getCompilationMXBean();
        System.out.println("getName: " + gm.getName());
        System.out.println("getTotalCompilationTime: " + gm.getTotalCompilationTime());

        //获取多个内存池的使用情况
        System.out.println("\n=======================MemoryPoolMXBean 获取多个内存池的使用情况============================ ");
        List<MemoryPoolMXBean> mpmList = ManagementFactory.getMemoryPoolMXBeans();
        for(MemoryPoolMXBean mpm : mpmList) {
            System.out.println("getName: " + mpm.getName());
            System.out.println("getType: " + mpm.getType());
            System.out.println("getUsage: " + mpm.getUsage());
            for (String memoryManagerName : mpm.getMemoryManagerNames()) {
                System.out.println("getMemoryManagerNames: " + memoryManagerName);
            }
        }
        //获取GC的次数以及花费时间之类的信息
        System.out.println("\n=======================MemoryPoolMXBean 获取GC的次数以及花费时间之类的信息============================ ");
        List<GarbageCollectorMXBean> gcmList = ManagementFactory.getGarbageCollectorMXBeans();
        for(GarbageCollectorMXBean gcm : gcmList) {
            System.out.println("getCollectionCount: " + gcm.getCollectionCount());
            System.out.println("getName: " + gcm.getName());
            for (String memoryPoolName : gcm.getMemoryPoolNames()) {
                System.out.println("getMemoryPoolNames: " + memoryPoolName);
            }
        }
        //获取运行时信息
        System.out.println("\n=======================RuntimeMXBean 获取运行时信息============================ ");
        RuntimeMXBean rmb = ManagementFactory.getRuntimeMXBean();
        System.out.println("getClassPath: " + rmb.getClassPath());
        System.out.println("getLibraryPath: " + rmb.getLibraryPath());
        System.out.println("getVmVersion: " + rmb.getVmVersion());
    }
}
